// JavaScript Document

/**
 * import compontent 
 */
Vsg.importClass(
	[
		'Vsg.Component.AttentionBlockGrid'
	]
);

Vsg.loadExtLibs([
	"ux/RowExpander.js"
	]
);


function getAnalyticalStocksPanel(url) {
	
	var sm = new Ext.grid.CheckboxSelectionModel({
		singleSelect:false
	});
			
    var expander = new Ext.ux.grid.RowExpander({
        tpl : new Ext.Template(
            '<p><b>出现种类/板块:</b> {assemblyCates}</p>'
        )
    });

	var store = new Ext.data.JsonStore({
    	// store configs
    	autoLoad : false,
		proxy:new Ext.data.HttpProxy({
			url:url,
			timeout:1000000
		}),
    	storeId: 'cateStockStore',
    	// reader configs
    	root: 'rows',
    	idProperty: 'stkCode',  
    	fields: [
			{name:'stockCode', mapping:'stockCode'},
			{name:'stockName', mapping:'stockName'},
			{name:'rate', type: 'float'},
			{name:'shownDate' , mapping:'lastShownDate'},
			{name:'assemblyCates', type:'string'},
			{name:'assemblyDates', type:'string'}
		]
	});
	
	try {
		// --- grid panel ---
  		var grid = new Ext.grid.GridPanel({
        	store: store,
			title:'分析后关注的股票',
			id:'attentionStkPanel',			
			maskDisabled:false,
			sm:sm,
        	loadMask: true,        	
        	// grid columns
			colModel:new Ext.grid.ColumnModel([
				sm,
				expander,
				{
					header: "股票代码",
					width: 100,
					sortable: false,
					dataIndex: "stockCode"
				},
				{
					header: "股票名称",
					sortable: false, 
					dataIndex: "stockName"
				},
				{
					header: "最近出现日期",
					sortable: false, 
					dataIndex: "shownDate"
				},				
				{
					header: "涨 幅",
					sortable: false, 
					dataIndex: "rate",
					renderer:function(val) {
						var result = (val*100).toFixed(2);
						return result+"%";
					}
				}								
			]),
        	// customize view config
        	viewConfig: {
            	forceFit:true
        	},
			plugins: expander,		
        	stripeRows: true

    	});
	
		return grid;	
	} catch (e) {
		alert("Get model panel error: ");
	}	
}


/** ===============================
 * init ui interface 
 * ================================
 */
function init(){
	Ext.QuickTips.init();

	try {
		
		// --- set layout content -----
		var $layoutContent = {
			layout:"border",
			items:[
				{
					layout:"border",
					region:'center',
					margins:"5 5 5 5 ",
					tbar:[{
						xtype:'button',
						iconCls: 'bmenu',
						text:'刷新/加载',
						tooltip:{title:'提示说明' , text:'刷新并加载预定的关注板块，加载的同时会清除旧的记录过滤'},
						menu:{
							items:[
								{
									text:'加载关注板块',
									handler:Vsg.getDefinedEvent("evt_load_atten_block")
									
								},
								{
									text:'清空关注股票',
									handler:Vsg.getDefinedEvent("evt_clear_atten_stock")
								},
								'-',
								{
									text:'加载并清空',
									handler:Vsg.getDefinedEvent("evt_clear&load")
								}								
							]
						}

					},{
						text:'分析筛选',
						handler:Vsg.getDefinedEvent("evt_filter_analysed")
					}],
					items:[
					{
						region:'west',
						width:430,	
						layout:'fit',
						split:true,
						margins:"5 0 5 5 ",
						border:false,
						items:[
							new Vsg.Component.AttentionBlockGrid({
								url: UrlMKey['attention-blocks'],
								title:'关注板块/种类',
								id:'attentionCate'
							}) 
						]
					},
					{
						region:'center',
						layout:'fit',
						border:false,
						margins:"5 5 5 0",
						items:[
							getAnalyticalStocksPanel(UrlMKey['attention-blocks-analysis-stocks'])
						]
					}					
					]
				}
			]
		};

    	new Ext.Viewport( $layoutContent );
		
	} catch (e) {
		alert('出现系统性错误，请联系管理人员!');
		//Ext.log("Initial UI error: " + e);
	}
}
// --- run the init method
Vsg.run( init );


/**
 * event
 * @param {Object} $this
 * @param {Object} rowIndex
 * @param {Object} event
 */
Vsg.definedEvent("evt_filter_analysed" , function($this, e) {
	var attentionCatePanel = Ext.getCmp("attentionCate");
	
	var selModel = attentionCatePanel.getSelectionModel();	
	var selRecs = selModel.getSelections();

	
	try {
		var ids = "";
		if (selModel.getCount() == 0) {
			throw "没有选择一条数据!";
		}
		for (var i = 0 ; i < selRecs.length ; i++) {
			if (i != 0) {
				ids+=",";
			}
			ids+=selRecs[i].id;
		}

		
		var panel = Ext.getCmp("attentionStkPanel");
		var store = panel.getStore();
		store.removeAll();
		store.load({
			params:{
				blockIds:ids
			}
		});		
		
	} catch ( ex ) {
		
		alert(ex);
	
	}
	
});


/**
 * 
 * @param {Object} $this
 * @param {Object} e
 */
Vsg.definedEvent("evt_load_atten_block" , function($this, e) {
	var panel = Ext.getCmp("attentionCate");
	var store = panel.getStore();
	store.removeAll();
	store.reload();
});

/**
 * 
 * @param {Object} $this
 * @param {Object} e
 */
Vsg.definedEvent("evt_clear_atten_stock" , function($this, e) {
	var panel = Ext.getCmp("attentionStkPanel");
	var store = panel.getStore();
	store.removeAll();
	
});

/**
 * 
 * @param {Object} $this
 * @param {Object} e
 */
Vsg.definedEvent("evt_clear&load" , function($this, e) {
	var acPanel = Ext.getCmp("attentionCate");
	var acStore = acPanel.getStore();
	acStore.removeAll();
	acStore.reload();

	var panel = Ext.getCmp("attentionStkPanel");
	var store = panel.getStore();
	store.removeAll();
	
});